﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Base.Entity;
using Newtonsoft.Json;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 领域对象元素
    /// </summary>
    [Serializable]
    public class CompositeNodeObjectElement //: SystemBase
    {
        private static bool isSetAliasAsID = false;

        static CompositeNodeObjectElement()
        {
            try
            {
                var config = ConfigurationManager.AppSettings["IsSetAliasAsID"];
                if (!string.IsNullOrEmpty(config) && config.ToLower() == "true")
                    isSetAliasAsID = true;
            }
            catch
            {
            }
        }

        /// <summary>
        /// ID
        /// </summary>
        public virtual string ID { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public virtual string Name { get; set; }

        private string displayName;
        /// <summary>
        /// 描述
        /// </summary>
        public string DisplayName
        {
            get
            {
                if (string.IsNullOrWhiteSpace(displayName))
                    displayName = Name;

                return displayName;
            }
            set
            {
                displayName = value;
            }
        }

        /// <summary>
        /// 标签
        /// </summary>
        private string alias;

        /// <summary>
        /// 标签
        /// </summary>        
        public virtual string Alias
        {
            get
            {
                if (isSetAliasAsID)
                    return ID;
                if (string.IsNullOrWhiteSpace(alias))
                    return Name;
                return alias;
            }
            set
            {
                alias = value;
            }
        }

        /// <summary>
        /// Normal|Reference类型的Elemet对应的DataColumn列
        /// </summary>
        public DataColumn DataColumn { get; set; }

        /// <summary>
        /// CDO关联的状态机
        /// </summary>
        public List<StateMachineMetadata> StateMachines { get; set; }

        /// <summary>
        /// 数据类型
        /// </summary>
        public virtual DataType DataType { get; set; }

        /// <summary>
        /// 长度
        /// </summary>       
        public virtual int Length { get; set; }

        /// <summary>
        /// 精度
        /// </summary>        
        public virtual int Precision { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>        
        public virtual string DefaultValue { get; set; }

        /// <summary>
        /// 默认值json
        /// </summary>        
        public virtual string DefaultValueJson { get; set; }

        /// <summary>
        /// 隶属的领域对象ID，暂时屏蔽该字段，运行时有问题再放开
        /// </summary>
        [JsonIgnore]      
        public virtual string CompositeNodeObjectID { get; set; }

        /// <summary>
        /// 隶属的领域模型ID
        /// </summary>
        [JsonIgnore]
        public string CompositeDataObjectID { get; set; }

        /// <summary>
        /// 元素类型
        /// </summary>
        public virtual ElementType ElementType { get; set; }

        /// <summary>
        /// 对应的数据列ID
        /// </summary>
        public virtual string DataColumnID { get; set; }

        /// <summary>
        /// 属性名称
        /// </summary>
        public virtual string PropertyName { get; set; }

        /// <summary>
        /// 属性类型
        /// </summary>
        public virtual string PropertyType { get; set; }

        /// <summary>
        /// 查询时是否必须
        /// </summary>
        public virtual bool IsForQuery { get; set; }
         
        /// <summary>
        /// 排序
        /// </summary>
        public virtual int Sort { get; set; }

        /// <summary>
        /// 能否为空，true:可以 false:不可以
        /// </summary>
        public bool IsAllowNull { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime LastModifyTime { get; set; }

        /// <summary>
        /// 返回表示当前对象的字符串。
        /// </summary>
        /// <returns>表示当前对象的字符串</returns>
        public override string ToString()
        {
            return Name;
            //            return string.Format("ID:{0}, Name:{1}, DataType:{2}, DataColumnID:{3}, PropertyName:{4}", ID, Name, DataType, DataColumnID, PropertyName);
        }

        /// <summary>
        /// 获取Element对应的DataColumn方法
        /// </summary>
        /// <param name="dmo"></param>
        /// <returns></returns>
        public DataColumn GetDataColumn(CompositeNodeObject dmo)
        {
            if (ElementType != ElementType.Normal)
                return null;
            var column = dmo.DataObject.Columns.FirstOrDefault(col => col.ID == this.DataColumnID);
            if (column == null)
                throw new Exception($"CDO:{this.CompositeDataObjectID}节点 id:{dmo.ID},code:{dmo.DataObjectTableAlias}列:{this.Alias ?? this.Name}在DO:{dmo.DataObject.Code}中找不到对应列:{this.DataColumnID}");
            return column;
        }
    }
}
